package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.model.entity.*;

import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        LeaseAgreement leaseAgreement = getById(id);
        String apartmentName = apartmentInfoService.getById(leaseAgreement.getApartmentId()).getName();
        List<GraphVo> apartmentList = graphInfoService.queryVoByApartmentId(leaseAgreement.getApartmentId());
        String roomNumber = roomInfoService.getById(leaseAgreement.getRoomId()).getRoomNumber();
        List<GraphVo> roomList = graphInfoService.queryVoByRoomId(leaseAgreement.getRoomId());
        String paymentTypeName = paymentTypeService.getById(leaseAgreement.getPaymentTypeId()).getName();
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        Integer monthCount = leaseTerm.getMonthCount();
        String unit = leaseTerm.getUnit();
        AgreementDetailVo vo=new AgreementDetailVo();
        vo.setApartmentName(apartmentName);
        vo.setLeaseTermUnit(unit);
        vo.setRoomNumber(roomNumber);
        vo.setPaymentTypeName(paymentTypeName);
        vo.setRoomGraphVoList(roomList);
        vo.setApartmentGraphVoList(apartmentList);
        vo.setLeaseTermMonthCount(monthCount);
        BeanUtils.copyProperties(leaseAgreement,vo);
        return vo;
    }

    @Override
    public List<AgreementItemVo> listAgreementItem(String userName) {
        LambdaQueryWrapper<LeaseAgreement> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(LeaseAgreement::getName,userName);
        List<LeaseAgreement> agreementList = list(wrapper);
        List<AgreementItemVo> list=new ArrayList<>(agreementList.size());
        for (LeaseAgreement agreement : agreementList) {
            List<GraphVo> graphVoList = graphInfoService.queryVoByRoomId(agreement.getRoomId());
            String roomNumber = roomInfoService.getById(agreement.getRoomId()).getRoomNumber();
            String apartmentName = apartmentInfoService.getById(agreement.getApartmentId()).getName();
            Boolean isRenewable=agreement.getStatus().equals(LeaseStatus.SIGNED)||agreement.getStatus().equals(LeaseStatus.RENEWING);
            AgreementItemVo agreementItemVo = new AgreementItemVo();
            agreementItemVo.setRoomNumber(roomNumber);
            agreementItemVo.setRoomGraphVoList(graphVoList);
            agreementItemVo.setApartmentName(apartmentName);
            agreementItemVo.setIsRenewable(isRenewable);
            BeanUtils.copyProperties(agreement,agreementItemVo);
            list.add(agreementItemVo);
        }
        return list;
    }
}




